package org.ielse.frame.test;

import org.ielse.frame.App;
import org.ielse.frame.BaseActivity;
import org.ielse.frame.C;
import org.ielse.frame.R;
import org.ielse.frame.model.AppSettings;
import org.ielse.frame.util.BaseUtil;
import org.ielse.frame.util.ImageUtil;
import org.ielse.frame.util.LogUtil;
import org.ielse.frame.view.ImageView;
import org.ielse.frame.view.SwitchView;
import org.ielse.frame.view.TitleLayout;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.os.Bundle;
import android.view.View;

public class NormalActivity extends BaseActivity {

	private TitleLayout layTitle;
	private SwitchView vsJustWifi;
	private AppSettings appSettings;
	
	private String testImageUrl = "http://a.hiphotos.baidu.com/news/q%3D100/sign=662de0a55643fbf2c32ca223807fca1e/b58f8c5494eef01f9a58dd68e5fe9925bd317dcb.jpg";

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_normal);
		super.onCreate(savedInstanceState);

		layTitle = (TitleLayout) findViewById(R.id.lay_title);
		layTitle.setTextTitle(R.string.action_settings);

		final ImageView imgTest = (ImageView) findViewById(R.id.img_test);
		imgTest.setType(ImageView.TYPE_ROUND);
		imgTest.loadImage("http://my.csdn.net/uploads/avatar/5/1/A/1_bfbx5173.jpg");

		imgTest.setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				int[] location = new int[2];
				v.getLocationOnScreen(location);
				int x = location[0];
				int y = location[1];
				LogUtil.d("imgTest2 .info:" + location[0] + "," + location[1]);
				startActivity(PictureActivity.class, PictureActivity.EXTRA_URL, "http://my.csdn.net/uploads/avatar/5/1/A/1_bfbx5173.jpg",
						PictureActivity.EXTRA_X, x, PictureActivity.EXTRA_Y, y - BaseUtil.getStatusBarHeight());
			}
		});

		final ImageView imgTest2 = (ImageView) findViewById(R.id.img_test2);
		imgTest2.setType(ImageView.TYPE_OVAL);
		imgTest2.loadImage(testImageUrl);

		imgTest2.setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				Bitmap picTransBack = getBitmapFromView(layContentWrapper, layContentWrapper.getWidth(), layContentWrapper.getHeight());
				ImageUtil.saveTofile(NormalActivity.this, picTransBack, C.settings.PICTURE_TRANS_BACK);
				
				int[] location = new int[2];
				v.getLocationOnScreen(location);
				int x = location[0];
				int y = location[1];
				startActivity(PictureActivity.class, PictureActivity.EXTRA_URL, testImageUrl,
						PictureActivity.EXTRA_X, x, PictureActivity.EXTRA_Y, y - BaseUtil.getStatusBarHeight());
			}
		});

		appSettings = App.i().getAppSettings();

		vsJustWifi = (SwitchView) findViewById(R.id.vs_just_wifi);
		boolean isJustWifi = App.i().getAppSettings().isDownloadImageJustWifi();
		vsJustWifi.setState(isJustWifi);
		vsJustWifi.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
			@Override public void toggleToOn() {
				vsJustWifi.toggleSwitch(true);
				appSettings.setDownloadImageJustWifi(true);
				appSettings.update(appSettings.getId());
			}

			@Override public void toggleToOff() {
				vsJustWifi.toggleSwitch(false);
				appSettings.setDownloadImageJustWifi(false);
				appSettings.update(appSettings.getId());
			}
		});

		final android.widget.ImageView imgCup = (android.widget.ImageView) findViewById(R.id.img_cup);

		findViewById(R.id.btn_cup).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View v) {
				Bitmap bm = getBitmapFromView(layContentWrapper,layContentWrapper.getWidth(),layContentWrapper.getHeight());
				LogUtil.d("bm size:" +bm.getByteCount() / 1024);
				imgCup.setImageBitmap(bm);
			}
		});
	}

	private Bitmap getBitmapFromView(View view, int width, int height) {
		int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
		int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
		view.measure(widthSpec, heightSpec);
		view.layout(0, 0, width, height);
		Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		canvas.drawColor(Color.TRANSPARENT);
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
		view.draw(canvas);
		return bitmap;
	}
}
